﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BeerManagementSystem
{
    /// <summary>
    /// Interaktionslogik für ReadCardDialog.xaml
    /// </summary>
    public partial class ReadCardDialog : Window
    {
        private ICardReader _cardReader;

        public String CardNewOwner { get; set; }

        public int CardIdentity { get; set; }

        LogWriter log;

        public ReadCardDialog(LogWriter log)
        {
            this.log = log;
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (CardNewOwner != null)
            {
                instructionLabel.Content = String.Format("Jetzt die Karte durchziehen, die \"{0}\" zugeordnet werden soll...", CardNewOwner);
            }
            else
            {
                instructionLabel.Content = "Jetzt die Karte durchziehen, die eingelesen werden soll...";
            }

#if DEBUG
            _cardReader = new EmulatedCardReader();
#else
            _cardReader = new MagTekCardReader();
#endif

            _cardReader.CardRead += new EventHandler<CardReadEventArgs>(_cardReader_CardRead);

            _cardReader.AcceptNextCard();
        }

        void _cardReader_CardRead(object sender, CardReadEventArgs e)
        {
            CardIdentity = e.CardIdentity;

            DialogResult = true;
            _cardReader.close();
            this.Close();
        }
    }
}
